<template>
	<view class="container" :class="petInfo?'circle-container':''">
		<u-navbar v-if="petInfo == null" title="E萌宠">
			<div slot="left">

			</div>
		</u-navbar>
		<u-navbar v-else title="E萌宠 " bgColor="transparent">
			<div slot="left" class="circle-box">
				<image style="width: 44rpx;height: 44rpx;border-radius: 50%;" :src="petInfo.avatar" mode=""></image>
				<span style="max-width: 160rpx;" class="text-ellipsis-1">{{petInfo.name}}</span>
				<image v-if="petInfo.sex == 1" style="width: 36rpx;height: 36rpx;" src="@/static/mine/female.png"
					mode="">
				</image>
				<image v-else style="width: 36rpx;height: 36rpx;" src="@/static/mine/male.png" mode="">
					<!-- <image style="width: 36rpx;height: 36rpx;" src="../../static/mine/male.png" mode=""></image> -->
			</div>
		</u-navbar>
		<image v-if="petInfo" class="bg-jb" style="width: 100%;"  src="../../static/img_jb.png"></image>
		<view class="content-wrap" style="">
			<view class="add-box" v-if="petInfo == null" @click="onAddPet">
				<image style="width: 100%;height:100%;" src="../../static/img_add.png"></image>
			</view>
			<cu-nav-bar class="cu-nav-bar-box" @clickItem="clickBar" :barList="barList"></cu-nav-bar>
			<cu-empty tips="请先登录~" v-if="!isLogin"></cu-empty>
			<template v-if="isLogin">
				<scroll-view class="content-box content-list" :scroll-top="scrollTop" scroll-y="true"
					@scrolltoupper="upper" @scrolltolower="lower" @scroll="scroll">
					<zero-waterfall :isHide="false" :isMask="false" @onItem="onItem" :list="listData" ref="waterFall"
						v-if="activeBar != 2">
						<view class="item" v-for="(item, index) of listData" :key="index" slot="slot{{item.id}}">
							<view class="title text-ellipsis">{{item.title}}</view>
							<view class="desc">
								<view class="font">
									<image style="width: 40rpx;height: 40rpx;border-radius: 50%;"
										:src="item.user_avatar" mode="">
									</image>
									<span class="name">{{item.nick_name}}</span>
								</view>
								<view class="views" v-if="item.browse">
									<image style="width: 28rpx;height: 28rpx;" src="../../static/icon_show.png" mode="">
									</image>
									<span class="browse">{{item.browse}}</span>
								</view>
							</view>
						</view>
					</zero-waterfall>
					<!-- 圈子 -->
					<view v-else>
						<view class="circle-list">
							<view @click="onJumpGroup(item)" class="item-box" v-for="(item,index) in listData"
								:key="item">
								<image class="img-avatar" :src="item.cover" mode=""></image>
								<view class="info">
									<view class="name">
										{{item.name}}  
									</view>
									<view class="tips">
										{{$Common.timeAgo(item.created_time) }} | {{item.nick_name}}
									</view>
								</view>
							</view>
						</view>
					</view>


					<cu-empty tips="这里没有发现什么~" v-if="listData && listData.length == 0"></cu-empty>
					<u-loadmore v-else :status="refreshStatus" />
				</scroll-view>
			</template>
		</view>

		<cu-button-wrap v-if="activeBar == 2 && isLogin">
			<u-button :customStyle="{width:'226rpx',height:'76rpx',borderRadius:'76rpx'}" type="primary" text="新增圈子"
				@click="onAddGroup"></u-button>
		</cu-button-wrap>
	</view>
</template>

<script>
	const iconShow = require('@/static/icon_show.png')

	import {
		mapState,
		mapActions
	} from 'vuex';
	import Refresh from '@/mixins/refresh'
	export default {
		mixins: [Refresh],
		components: {

		},
		data() {
			return {
				iconShow,
				status: 'loadmore',
				scrollTop: 0,
				listData: null,
				groupList: null,
				activeBar: 1,
				barList: [{
						label: '关注',
						id: 0
					},
					{
						label: '综合',
						id: 1
					},
					{
						label: '圈子',
						id: 2
					},
					{
						label: '专题',
						id: 3
					},
				],
				circle: null
			}
		},
		computed: {
			...mapState(['userInfo', 'petInfo', 'isLogin']),
		},
		onShow() {
			if (this.isLogin) {
				this.getPetList();
			}
		},
		onLoad() {
			if (this.isLogin) {
				this.load_user_atten_user_post(false);
			}
			// wx.showTabBarRedDot({
			// 	index: 2,
			// });
			// wx.hideTabBarRedDot({
			//   index: 0,
			// });
			// this.loadList();
			let _this = this;
			// 更新圈子
			uni.$on('updateGroupList', () => {
				_this.getGroupList();
				_this.activeBar = '2';
			})

		},


		destroyed() {
			uni.$off('updateGroupList')
		},
		methods: {
			...mapActions(['setPetInfo']),
			onAddPet(){
				if (this.isLogin) {
					this.$Jump.Open('/pages/mine/pet/add')
				}else{
					this.$Msg.Toast('请先登录')
				}
			},
			onItem(e) {
				if (this.activeBar == 3) {
					this.$Jump.Open('/pages/index/articleDetail?id=' + e.id + "&type=" + this.activeBar)
				} else {
					this.$Jump.Open('/pages/index/postDetail?id=' + e.id + "&type=" + this.activeBar)
				}

			},
			// 综合
			load_post_news(status) {
				this.$Net.get(this.$api.post_news, {
					...this.searchReq
				}, 1).then(res => {
					res.list.map(item => {
						if (item.cover) {
							item['url'] = item.cover;
						} else if (item.imgs) {
							item['url'] = item.imgs.split(",")[0];
						} else {
							item['url'] = ''
						}
						return item
					});
					// this.status = 'nomore';
					this.setStatus(res.count, res.list, status);
					if (!status) {
						this.$refs.waterFall.init()
					}
				})
			},
			// 关注
			load_user_atten_user_post(status) {
				this.$Net.get(this.$api.user_atten_user_post, {
					...this.searchReq
				}, 1).then(res => {
					res.list.map(item => {
						if (item.cover) {
							item['url'] = item.cover;
						} else if (item.imgs) {
							item['url'] = item.imgs.split(",")[0];
						} else {
							item['url'] = ''
						}
						return item
					});
					this.setStatus(res.count, res.list, status);
					console.log(this.listData);
					this.$refs.waterFall.init()

					// this.status = 'nomore';
					// this.$refs.waterFall.init()
				})
			},
			// 专题
			getArticleList(status) {
				this.$Net.get(this.$api.article_list + "/1", {
					...this.searchReq
				}, 1).then(res => {
					res.list.map(item => {
						if (item.image_input && item.image_input.length > 0) {
							item['url'] = item.image_input[0];
						}else{
							item['url'] = ''
						}
						// item['url'] = item.image_input;						
						return item
					});
					this.setStatus(res.count, res.list, status);
					this.$refs.waterFall.init()

					// this.status = 'nomore';
					// this.$refs.waterFall.init()
				})
			},
			// 宠物
			getPetList() {
				this.$Net.get(this.$api.pet_List, {
					page: 1,
					limit: 1
				}, 1).then(res => {
					if (res.list && res.list.length > 0) {
						this.setPetInfo(res.list[0]);
						// this.circle = res.list[0]
					}else{
						this.setPetInfo(null);
					}
				})
			},
			// 新增圈子
			onAddGroup() {
				this.$Jump.Open('/pages/mine/circle/addCircle')
			},
			// 圈子详情
			onJumpGroup(item) {
				console.log(item);
				this.$Jump.Open('/pages/index/gruop?id=' + item.id)
				// pages/index/gruop
			},
			// 加载圈子列表
			getGroupList(status) {
				this.$Net.get(this.$api.group_list, {
					...this.searchReq
				}).then(res => {
					this.setStatus(res.count, res.list, status);
				})
			},
			loaded() {
				console.log('加载完成')
			},
			wapperClick(item) {
				this.$Jump.Open('/pages/index/postDetail')
				console.log('单项点击事件', item)
			},
			imageClick(item) {
				console.log('图片点击事件', item)
				this.$Jump.Open('/pages/index/postDetail')
			},
			reset() {
				this.listData = null
				this.$refs.waterfallsFlowRef.refresh();
			},

			loaded: function(e) {
				console.log(e)
			},
			upper: function(e) {
				// this.fnRefresherrefresh();
			},
			lower: function(e) {
				this.scrolltolower()
			},
			scroll: function(e) {
				// console.log(e)
				// this.scrollTop = e.detail.scrollTop
			},
			getList(status) {
				this.list = null;
				if (this.activeBar == 2) {
					this.getGroupList(status);
				} else if (this.activeBar == 0) {
					this.load_user_atten_user_post(status);
				} else if (this.activeBar == 1) {
					this.load_post_news(status);
				} else if (this.activeBar == 3) {
					this.getArticleList(status);
				}
			},
			clickBar(id) {
				this.resetPage();
				this.listData = []
				this.$refs.waterFall?.clear();
				this.activeBar = id;
				if (this.isLogin) {
					this.getList(false);
				}
			},
			// getList() {
			// 	this.status = 'loading';
			// 	this.$Net.post(this.$api.myClockInfo4Today, {}).then(res => {
			// 		console.log(res)
			// 		if (res.success) {
			// 			this.listAttendance = res.body
			// 			this.status = 'nomore';
			// 		}
			// 	})
			// },
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 0;

		::v-deep .btn-box {
			z-index: 99;
		}
	}

	.circle-container {
		::v-deep .u-navbar__content__title {
			color: #fff !important;
		}

		.circle-box {
			min-width: 188rpx;
			height: 64rpx;
			border-radius: 64rpx;
			background: rgba(0, 0, 0, .15);
			display: flex;
			align-items: center;
			padding: 0 10rpx;

			span {
				margin-left: 8rpx;
				margin-right: 6rpx;
				color: rgba(255, 255, 255, 0.90);
				text-align: center;
				font-family: "PingFang SC";
				font-size: 32rpx;
				font-weight: 400;
				line-height: 64rpx;
			}
		}

		.content-wrap {
			border-radius: 40rpx 40rpx 0 0;
			margin-top: 240rpx !important;
		}
	}

	.bg-jb {
		position: absolute;
		top: 0;
		z-index: 2;
	}

	.content-wrap {
		height: calc(100vh - 120px);
		display: flex;
		flex-direction: column;
		padding: 0 32rpx;
		margin-top: 200rpx;
		position: relative;
		z-index: 3;
		background-color: #fff;

		.add-box {
			width: 694rpx;
			height: 272rpx;
			flex-shrink: 0;
			border-radius: 56rpx;
			background: #68B0FC;
			box-shadow: 0px 42rpx 28rpx -30rpx #C4DDF6;
		}



		.content-box {
			width: 100%;
			flex: 1;
			overflow-y: auto;

			::v-deep .img {
				border-radius: 28rpx !important;
			}

			.item {
				width: 100%;
				padding-top: 18rpx;
				// margin-bottom: 20rpx;

				.image {
					background-color: #C4DDF6;
				}

				.title {
					color: rgba(0, 0, 0, 0.80);
					font-family: "PingFang SC";
					font-size: 30rpx;
					font-weight: 400;
				}

				.desc {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 12rpx;
					width: 100%;
					color: rgba(0, 0, 0, 0.50);
					font-family: "PingFang SC";
					font-size: 26rpx;

					.font,
					.views {
						display: flex;
						align-items: center;

						span {
							margin-left: 12rpx;
						}
					}
				}
			}
		}

	}

	.circle-list {
		.item-box {
			margin-bottom: 50rpx;
			border-radius: 16px;
			display: flex;
			align-items: center;

			.img-avatar {
				width: 72rpx;
				height: 72rpx;
				margin-right: 28rpx;
				border-radius: 32rpx;
				flex-shrink: 0;
			}

			.info {
				.name {
					color: #000;
					font-family: "PingFang SC";
					font-size: 36rpx;
					font-weight: 400;
				}

				.tips {
					color: rgba(0, 0, 0, 0.60);
					font-family: "PingFang SC";
					font-size: 28rpx;
					font-weight: 400;
					margin-top: 12rpx;
				}
			}
		}
	}

	::v-deep .btn-box {
		padding-bottom: 20rpx !important;
	}
</style>